﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using Microsoft.Practices.Unity;
using LCA.Community.DAL.Test.ModelFactories;

namespace LCA.Community.DAL.Test
{
    
    
    /// <summary>
    ///This is a test class for AnswersRepositoryTest and is intended
    ///to contain all AnswersRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AnswersRepositoryTest : BaseTestClass
    {
        private UsersFactory _usersFactory = new UsersFactory();
        private QuestionsFactory _questionsFactory = new QuestionsFactory();
        private AnswersFactory _answersFactory = new AnswersFactory();
        private List<User> _users;
        private List<Question> _questions;
        private List<Answer> _answers;

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            _users = _usersFactory.CreateList(5);
            _questions = _questionsFactory.CreateList(5, _users[0].RowKey);
            _answers = _answersFactory.CreateList(10, _questions[0].RowKey, _users[0].RowKey);

            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            _users.ForEach(user => usersRepository.Add(user));

            var questionsRepository = _unityContainer.Resolve<IQuestionsRepository>();
            _questions.ForEach(question => questionsRepository.Add(question));

            var answersRepository = _unityContainer.Resolve<IAnswersRepository>();
            _answers.ForEach(answer => answersRepository.Add(answer));
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var answersRepository = _unityContainer.Resolve<IAnswersRepository>();
            var questionsRepository = _unityContainer.Resolve<IQuestionsRepository>();
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();

            answersRepository.DeleteTestEntities();
            questionsRepository.DeleteTestEntities();
            usersRepository.DeleteTestEntities();
        }        

        /// <summary>
        ///A test for GetAnswersByQuestion
        ///</summary>
        [TestMethod()]
        public void GetAnswersByQuestionTest()
        {
            var target = _unityContainer.Resolve<IAnswersRepository>();
            string questionID = _questions[0].RowKey;

            List<Answer> actual;
            actual = target.GetAnswersByQuestion(questionID);

            CollectionAssert.AreEquivalent(_answers, actual);
        }

        /// <summary>
        ///A test for GetAnswersByQuestion
        ///</summary>
        [TestMethod()]
        public void GetAnswersByQuestionTest1()
        {
            var target = _unityContainer.Resolve<IAnswersRepository>();
            string questionID = _questions[0].RowKey;

            int page = 0; 
            int pageSize = 5;

            List<Answer> actual;
            actual = target.GetAnswersByQuestion(questionID, page, pageSize);

            CollectionAssert.AreEquivalent(_answers.Take(5).ToList(), actual);

            page++;
            actual = target.GetAnswersByQuestion(questionID, page, pageSize);

            CollectionAssert.AreEquivalent(_answers.Skip(5).ToList(), actual);

            page++;
            actual = target.GetAnswersByQuestion(questionID, page, pageSize);

            CollectionAssert.AreEquivalent(new List<Answer>(), actual);
        }

        /// <summary>
        ///A test for GetAnswersByUser
        ///</summary>
        [TestMethod()]
        public void GetAnswersByUserTest()
        {
            var target = _unityContainer.Resolve<IAnswersRepository>();        
            string userID = _users[0].RowKey; 

            List<Answer> actual;
            actual = target.GetAnswersByUser(userID);

            CollectionAssert.AreEquivalent(_answers, actual);
        }

        /// <summary>
        ///A test for DeleteAllUserAnswers
        ///</summary>
        [TestMethod()]
        public void DeleteAllUserAnswersTest()
        {
            var target = _unityContainer.Resolve<IAnswersRepository>();
            string userID = _users[0].RowKey;
            
            target.DeleteAllUserAnswers(userID);
            var actual = target.GetAnswersByUser(userID);

            CollectionAssert.AreEquivalent(new List<Answer>(),actual);
        }

        /// <summary>
        ///A test for DeleteAllQuestionAnswers
        ///</summary>
        [TestMethod()]
        public void DeleteAllQuestionAnswersTest()
        {
            var target = _unityContainer.Resolve<IAnswersRepository>();

            string questionID = _questions[0].RowKey;
            target.DeleteAllQuestionAnswers(questionID);

            var actual = target.GetAnswersByQuestion(questionID);

            CollectionAssert.AreEquivalent(new List<Answer>(), actual);
        }
    }
}
